package com.wanmait.hr.service.Impl;


import com.wanmait.hr.service.PictureService;
import com.wanmait.hr.util.FileUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
@Service
public class PictureServiceImpl implements PictureService {

    @Override
    public String showPic(MultipartFile photo, HttpServletRequest request, FileUtils fileUtils) {
        String fileName = photo.getOriginalFilename();
        String ext =fileUtils.getFileExt(fileName);
        String newName = UUID.randomUUID().toString();
        if(!ext.equals("")){
            newName+="."+ext;
        }
        String realPath = request.getServletContext().getRealPath("/static/images")+"/"+newName;

        File file = new File(realPath);
        try {

            photo.transferTo(file);


            return newName;

        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;

    }
}
